import time
import sys
import random
from monster import Monster


def begin():
    print('1.继续游戏')
    print('2.新游戏')
    print('3.退出游戏')
    e = input('请选择：')
    if e == '1':
        congame()
        choose()
    if e == '2':
        # face()
        start()
    elif e == '3':
        sys.exit()
    else:
        print('乱输入会降低极品装爆率，请慎重选择!')


def start():
    global heron
    print('*'*80)
    print('一觉醒来，你已经置身与一个巨大无比的幼儿园中，门口赫然写着：'.center(60))
    time.sleep(1)
    print('#####吉##山##幼##儿##园#####'.center(60))
    time.sleep(1)
    print("天空中，传来一声巨响".center(60))
    time.sleep(1)
    print('程序猿啊~~~~~~~'.center(60))
    time.sleep(1)
    print('我是有趣的恶魔♂~~~~~'.center(60))
    time.sleep(1)
    print('你已经变成一只蚂蚁大小~~~~~~'.center(60))
    print('现在你将为了解开封印而战斗~~~~~~'.center(60))
    time.sleep(2)
    heron = input('（恶魔说话为什么总爱拖长音……）现在，给自己起个勇敢的名字吧：')
    print('%s,你已经具备了一个勇士的资格，接下来你会获得一些装备，去背包装备他们吧' % heron)
    # print('这些装备的属性很低，但是足够你撑过前期了~')
    print('你可以强化他们或者获得更强力的装备，并且最终斩首恶魔，也有可能半路夭折，祝你好运~┏(＾0＾)┛')


def choose():
    print('*' * 30)
    print('1.前进')
    print('2.休息')
    print('3.升级装备')
    print('4.照一下镜子')
    x = input('%s少侠，请做出你的选择**********:'% heron)
    if x == '1':
        battle()
    elif x == '2':
        rest()
    elif x == '3':
        weapon()
    elif x == '4':
        option()
    else:
        print('乱输入会降低极品装爆率，请慎重选择!')


def battle():
    global cs
    print('1.大门口（lv1--lv10）')
    print('2.喷泉（lv11--lv20）')
    print('3.滑梯（lv21--lv30）')
    print('4.操场（lv31--lv40）')
    print('5.教室（lv41--lv50）')
    print('6.校长室（lv50以上）')
    cs = input('选择当前街区：')
    if cs.isdigit() and int(cs) in range(1, 7):
        cs = int(cs)
        n = Monster(cs)
        print('你遇到了%s' % n.name)
        hero.fight(n)


class Hero(object):
    def __init__(self):
        self.lif = 100
        self.atk = 10
        self.arm = 2
        self.exp = 0

    def fight(self, aim):
        save()
        print('已经自动存档~~~')
        global lev
        global ms
        while aim.lif > 0:
            aim.lif -= self.atk
            print('你挥出%s,对%s造成%d的伤害,%s还有%d生命值' % (equip['武器'], aim.name, self.atk, aim.name, aim.lif))
            if aim.lif <= 0:
                print('勇敢的%s啊，你击败了怪兽！' % heron)
                print('%s少侠，你还有%d苟且值' % (heron, self.lif))
                self.exp += aim.exp
                print('获得%d点经验值' % aim.exp)
                if lev < 10:
                    if lev < 1 + self.exp // 100:
                        lev = 1 + self.exp // 100
                        self.atk += 10 + lev*2
                        self.arm += 2 + lev*2
                        self.lif = self.lif + 10
                        print('升级成功！当前等级：%d' % lev)
                elif 10 <= lev < 20:
                    if lev < self.exp // 1000:
                        lev = self.exp // 1000
                        self.atk += 10 + lev*2
                        self.arm += 2 + lev*2
                        self.lif = self.lif + 100
                        print('升级成功！当前等级：%d' % lev)
                elif 20 <= lev < 30:
                    if lev < self.exp // 10000:
                        lev = self.exp // 10000
                        self.atk += 10 + lev*2
                        self.arm += 2 + lev*2
                        self.lif = self.lif + 100
                        print('升级成功！当前等级：%d' % lev)
                elif 30 <= lev < 40:
                    if lev < self.exp // 100000:
                        lev = self.exp // 100000
                        self.atk += 10 + lev*2
                        self.arm += 2 + lev*2
                        self.lif = self.lif + 100
                        print('升级成功！当前等级：%d' % lev)
                elif 40 <= lev < 50:
                    if lev < self.exp // 1000000:
                        lev = self.exp // 1000000
                        self.atk += 10 + lev*2
                        self.arm += 2 + lev*2
                        self.lif = self.lif + 100
                        print('升级成功！当前等级：%d' % lev)
                elif lev >= 50:
                    print('已达最大等级，杀怪将不再获得经验值~')
                rewards(random.randint(0, 2))
                break
            self.lif = self.lif + self.arm - aim.atk
            print('怪兽咬了你%d的苟且值' % (aim.atk-self.arm))
            if self.lif <= 0:
                print('苟不住了，被%s打死了……you died~~~' % aim.name)
                if ms < 1:
                    sys.exit()
                else:
                    ms -= 1
                    print('你死了，但是别怕，还有复活的机会~')
                    print('目前还有%d条命,苟且值已经恢复到满状态，继续战斗吧%s' % (ms, heron))
                    self.lif = 100*lev

    def equipa(self, equipment):
        self.arm = 2 + 2*lev + equipment.arm
        equip['护甲'] = equipment
        print('%s装备成功！防御力现在为%d' % (equipment, self.arm))

    def equipw(self, equipment):
        self.atk = 10 + 2 * lev + equipment.atk
        equip['武器'] = equipment
        print('%s装备成功！攻击力变更为%d' % (equipment, self.atk))

    def __str__(self):
        return ('当前你的苟且力：%d\n当前你的攻击力：%d\n当前你的防御力：%d\n当前等级：%d\n'%(self.lif,self.atk,self.arm,lev))


def rewards(i):
    global gd
    global x
    if i == 0:
        gd += lev*10
        print('获得金币%d个，现有金币%d个'%(lev*10,gd))
    elif i == 1 :
        x = EquipW()
        bag.append(x)
        print('获得武器：%s' % x.name)
    elif i == 2:
        x = EquipA()
        bag.append(x)
        print('获得防具：%s' % x.name)


def save():
    f = open('%s.txt' % heron, 'w+')
    f.write(str(lev)+'\n')
    f.write(str(hero.lif)+'\n')
    f.write(str(hero.exp)+'\n')
    f.write(str(hero.atk)+'\n')
    f.write(str(hero.arm)+'\n')
    f.write(str(len(bag))+'\n')
    for i in bag:
        f.write(i.sx + '\n')
        if i.sx == 'w':
            f.write(str(i.atk)+'\n')
        else:
            f.write(str(i.arm)+'\n')
        f.write(str(i.name) + '\n')
    f.write(str(equip['武器'])+'\n')
    f.write(str(equip['护甲'])+'\n')
    f.write(str(gd)+'\n')
    f.write(str(ms))
    f.close()





def congame():
    global heron
    global lev
    global equip
    global bag
    global gd
    global hero
    global ms
    ms = 10
    gd = 0
    bag = []
    hero = Hero()
    equip = {'武器': '', '护甲': ''}
    heron = input('请输入英雄名来读档')
    try:
        f = open('%s.txt' % heron, 'r+')
        contents = f.readlines()
        lev = int(contents[0])
        hero.lif = int(contents[1])
        hero.exp = int(contents[2])
        hero.atk = int(contents[3])
        hero.arm = int(contents[4])
        n = int(contents[5])
        if n > 0:
            for i in range(n):
                if contents[6 + 3*i] == 'w\n':
                    e = EquipW()
                    new_contents = contents[7 + 3 * i].replace('\n', '')
                    e.atk = int(new_contents)
                else:
                    e = EquipA()
                    new_contents = contents[7 + 3 * i].replace('\n', '')
                    e.arm = int(new_contents)
                new_contents = contents[8 + 3*i].replace('\n','')
                e.name = new_contents
                bag.append(e)
        for i in bag:
            new_contents = contents[-4].replace('\n', '')
            if i.name == new_contents:
                equip['武器'] = i
            new_contents1 = contents[-4].replace('\n', '')
            if i.name == new_contents1:
                equip['护甲'] = i
        gd = int(contents[-2])
        ms = int(contents[-1])+1
        f.close()
        print('读档成功！赠送一条生命~❤')

    except Exception:
        print('没有你的英雄~~')


class EquipA(object):
    def __init__(self):
        equipaNames = ['虎爪衣', '虎皮衣', '地玄裘', '苏锦衣', '灭寒盔', '棉布衣', '不详之兆', '红莲斗篷', '守护者之铠', '霸者重装',
                       '帝王战甲', '魔神甲', '虎啸战甲', '暗咒道甲', '烈焰魔衣', '赤羽衣', '轻羽袍', '霓裳羽衣', '孤月', '道神逆火'
                       '狂徒铠甲', '守护天使', '紫金七星冠', '锁子黄金甲', '花蝶舞', '魂魄断', '碧空之歌', '格萨拉克·地渊之声', '既定之天命', '无尽寒冬',
                       '五彩宝莲衣', '霜之哀伤', '临兵斗者皆诡道', '大天使之击', '黑暗湮灭', '兄弟会之盾', '暴闪环带', '回音盔甲',
                       '混天绫', '凤翅瑶仙簪', '蝙蝠侠套装', '阿尔忒弥斯的拥抱', '幻影夫人的轻抚', '孔雀屏', '亡骑士之先锋盾', '米萨格利亚斯套装', '加十五超强力', '英雄胆']
        if lev < 10:
            self.name = equipaNames[random.randint(0,10)]
            self.arm = random.randint(4, 10)
            self.sx = 'a'
        elif 10 <= lev < 20:
            self.name = equipaNames[random.randint(11,20)]
            self.arm = random.randint(40, 70)
            self.sx = 'a'
        elif 20 <= lev < 30:
            self.name = equipaNames[random.randint(21,30)]
            self.arm = random.randint(400, 700)
            self.sx = 'a'
        elif 30 <= lev < 40:
            self.name = equipaNames[random.randint(31,40)]
            self.arm = random.randint(4000, 7000)
            self.sx = 'a'
        elif 40 <= lev < 50:
            self.name = equipaNames[random.randint(41,50)]
            self.arm = random.randint(40000, 70000)
            self.sx = 'a'
        elif lev >= 50:
            self.name = equipaNames[random.randint(0, 50)]
            self.arm = random.randint(40000, 70000)
            self.sx = 'a'

    def __str__(self):
        return '护甲：%s,防御力+%d' % (self.name, self.arm)


class EquipW(object):
    def __init__(self):
        equipNames = ['匕首', '铁剑','乌木剑', '青铜斧', '魔杖', '符纹半月', '斩马刀', '修罗', '符纹降魔', '多兰之刃',
                      '精钢修罗', '宣花斧', '枯骨刀', '提亚马特', '无极棍', '命运之刃', '龙纹剑', '重钢裁决', '嗜魂法杖', '逍遥扇',
                      '屠龙刀', '斩妖剑', '缚龙索', '无尽之刃', '饮血剑', '倚天剑', '混天盖地槊', '皇天怒', '方天画戟', '圣火令', '十八铜人的折凳（五号）',
                      '赤炼鬼爪', '震天戟', '盘古锤', '玄天破邪杖', '霸王丈八矛', '真武朱雀扇', '莫邪', '干将', '如意神剑', '无上之剑', '轩辕剑',
                      '如意金箍棒', '芭蕉扇', '降妖宝杖', '乾坤钵', '凡性的觉醒', '灵能扑克', '修罗斩', '阎魔冥谕', '皓月苍穹', '玄极涅云千华闪']
        if lev < 10:
            self.name = equipNames[random.randint(0, 1)]
            self.atk = random.randint(4, 10)
            self.sx = 'w'
        elif 10 <= lev < 20:
            self.name = equipNames[random.randint(11, 20)]
            self.atk = random.randint(40, 70)
            self.sx = 'w'
        elif 20 <= lev < 30:
            self.name = equipNames[random.randint(21, 30)]
            self.atk = random.randint(400, 700)
            self.sx = 'w'
        elif 30 <= lev < 40:
            self.name = equipNames[random.randint(31, 40)]
            self.atk = random.randint(4000, 7000)
            self.sx = 'w'
        elif 40 <= lev < 50:
            self.name = equipNames[random.randint(41,50)]
            self.atk = random.randint(40000, 70000)
            self.sx = 'w'
        elif lev >= 50:
            self.name = equipNames[random.randint(0, 50)]
            self.atk = random.randint(40000, 70000)
            self.sx = 'w'

    def __str__(self):
        return '武器：%s,攻击力+%d' % (self.name,self.atk)


def rest():
    if hero.lif < 100*lev:
        hero.lif += hero.lif + 11 * lev
        print('苟且值得到了一些恢复')
    else:
        print('苟且值已达到最大，%d' % hero.lif)


def info(i):
    if i == 1:
        print('*'*30)
        print('英雄名字：%s' % heron)
        print('当前等级：%d' % lev)
        print('当前经验：%d' % hero.exp)
        print('当前剩余生命数：%d' % ms)
        print(hero)
        return
    elif i == 2:
        if len(bag) == 0:
            print('背包里面啥也没有！')
            return
        else:
            for j in range(0, len(bag)):
                print(j+1, ' ', bag[j])
                if bag[j] == equip['武器'] or bag[j] == equip['护甲']:
                    print('(已装备)', end='')
                print()
            x = input('输入"q"返回,或直接按下Enter进入装备操作：')
            if x == 'q':
                return
            else:
                if lev < 10:
                    ne = input('输入物品序号操作装备：')
                    if ne.isdigit():
                        if int(ne)-1 > len(bag):
                            print('你的背包没有那么多东西！')
                        else:
                            e = int(ne)-1
                            c = input('请输入要进行的操作：1.装备 2.出售 3.销毁')
                            if c == '1':
                                if bag[e].sx == 'a':
                                    hero.equipa(bag[e])
                                if bag[e].sx == 'w':
                                    hero.equipw(bag[e])
                            elif c == '2':
                                if lev < 10:
                                    print('lv10解锁批量出售!')
                                bag.remove(bag[e])
                                print('出售成功！')
                                rewards(0)
                            elif c == '3':
                                bag.remove(bag[e])
                                print('销毁成功！')
                            else:
                                return
                    else:
                        print('乱操作会降低爆率，请重新输入！')
                if lev >= 10:
                    ne = input('输入物品序号操作装备：')
                    if ne.isalnum():
                        if int(ne) > len(bag):
                            print('你的背包没有那么多东西！')
                        else:
                            e = int(ne)-1
                            c = input('请输入要进行的操作：1.装备 2.出售 3.出售所有非装备物品 4.销毁')
                            if c == '1':
                                if bag[e].sx == 'a':
                                    hero.equipa(bag[e])
                                if bag[e].sx == 'w':
                                    hero.equipw(bag[e])
                            elif c == '2':
                                bag.remove(bag[e])
                                print('出售成功！')
                                rewards(0)
                            elif c == '3':
                                for j in bag:
                                    if not (j == equip['武器'] or j == equip['护甲']):
                                        bag.remove(j)
                                        rewards(0)
                                print('出售成功！')
                            elif c == '4':
                                bag.remove(bag[e])
                                print('销毁成功！')
                            else:
                                return
                    else:
                        print('乱操作会降低爆率，请重新输入！')
    elif i == 3:
        for j in equip.keys():
            print(j, end=':')
            print(equip[j])
        return
    elif i == 4:
        print('当前拥有金币为：%d'%gd)
        return


def option():
    while True:
        print('*' * 30)
        print('*' * 10 + '1.查看属性')
        print('*' * 10 + '2.查看背包')
        print('*' * 10 + '3.查看装备')
        print('*' * 10 + '4.查看金币')
        print('*' * 10 + '5.回到游戏')
        print('*' * 10 + '6.存档退出')
        print('*' * 10 + '7.直接退出')
        e = input('%s少侠，请做出你的选择**********:' % heron)
        if e == '1':
            info(1)
        elif e == '2':
            info(2)
        elif e == '3':
            info(3)
        elif e == '4':
            info(4)
        elif e == '5':
            break
        elif e == '6':
            save()
            print('存档成功，大爷再来玩呀~~❤~~❤~~❤~~❤~~❤~~❤~~❤~~')
            sys.exit()
        elif e == '7':
            sys.exit()
        else:
            print('乱输入会降低极品装爆率，请慎重选择!')


def weapon():
    global gd
    if lev < 4:
        print('欢迎来到强大的武器打造系统，如果你的欧气不够，打造系统,将是你获得极品装备的最佳途径！')
    for j in range(0, len(bag)):
        print(j+1, ' ', bag[j])
    i = input('根据装备等级不同，打造所消耗的金币也不同，打造不可逆，产出不固定，按“Y”确认，打造吗?')
    if i.lower() == 'y':
        e = int(input('请输入你要打造的装备序号：'))-1
        if bag[e].sx == 'a':
            if gd > 300*lev:
                bag[e].arm *= 1.1*random.randint(1, 3)
                print('打造成功！')
                gd -= 300*lev
                if equip['护甲'] == bag[e]:
                    hero.arm += bag[e].arm
            else:
                print('钱不够！')
        elif bag[e].sx == 'w':
            if gd > 300 * lev:
                bag[e].atk *= 1.1*random.randint(1,3)
                print('打造成功！')
                gd -= 300 *lev
                if equip['武器'] == bag[e]:
                    hero.atk += bag[e].atk
            else:
                print('钱不够！')
    else:
        return


ms = 10
gd = 0
lev = 1
bag = []
equip = {'武器': '', '护甲': ''}

hero = Hero()
begin()
while True:
    choose()


